import Feature from 'ol/Feature'
import LinearRing from "ol/geom/LinearRing"
import { isArray } from '../utils/index'
import { v4 as uuidv4 } from 'uuid'
/**
 * 
 * @param {Array} ringArr
 * @returns {Object}
 */
export const lineRing = (ringArr) => {
  if (!isArray(ringArr) || !ringArr.length) {
    throw Error('绘制lineRing时参数ringArr格式错误')
  }
  const ids = []
  const features = ringArr.map((item) => {
    const {
      id = uuidv4(),
      name = '',
      data
    } = item || {}
    ids.push(id)

    if (!isArray(data) || !data.length) {
      throw Error('绘制lineRing：传入参数data格式错误，必须为经纬度数组')
    }
    const geometry = new LinearRing(data)
    return new Feature({
      id,
      name,
      type: 'linearRing',
      property: { ...item },
      geometry
    })

  })
  return {
    features,
    ids
  }
}